package la.iok.aone.monitor.youtube.mapper;

import la.iok.aone.monitor.youtube.model.YoutubeLink;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
public interface YoutubeLinkMapper {

    @Select("SELECT * FROM youtube WHERE del_flag = '0'")
    List<YoutubeLink> getAll();

    @Select("SELECT * FROM youtube WHERE del_flag = '0' limit #{begin}, #{end}")
    List<YoutubeLink> getAllByPage(@Param("begin")int begin, @Param("end")int end);

    @Select("<script>SELECT * FROM youtube WHERE <if test=\"category!=null and category != ''\">category like CONCAT('%',#{category},'%') AND </if> checked = '1' AND del_flag = '0' limit #{begin}, #{end}</script>")
    List<YoutubeLink> getByPage(@Param("category")String category, @Param("begin")int begin, @Param("end")int end);

    @Select("SELECT * FROM youtube WHERE monitor_count < #{count} AND checked = '1' AND del_flag = '0' limit #{pageSize} for update")
    List<YoutubeLink> selectForPageMonitCountLess(@Param("count") int count, @Param("pageSize") int pageSize);

    @Select("SELECT * FROM youtube WHERE checked = '1' AND del_flag = '0' limit #{pageSize} for update")
    List<YoutubeLink> selectForPage(int pageSize);

    @Select("SELECT * FROM youtube WHERE id = #{id} AND del_flag = '0'")
    @Results({
            @Result(property = "videoId", column = "video_id"),
            @Result(property = "checkTime", column = "check_time"),
            @Result(property = "errorMsg", column = "error_msg"),
            @Result(property = "monitorCount", column = "monitor_count"),
            @Result(property = "monitorTime", column = "monitor_time"),
            @Result(property = "delFlag", column = "del_flag"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    YoutubeLink findById(int id);

    @Select("SELECT * FROM youtube WHERE link = #{link}")
    YoutubeLink findByLink(String link);

    @Select("SELECT * FROM youtube WHERE feedback_id = #{feedbackId}")
    YoutubeLink findByFeedbackId(String feedbackId);

    @Select("SELECT count(1) FROM youtube")
    int countAll();

    @Select("SELECT count(1) FROM youtube WHERE del_flag = '0'")
    int count();

    @Select("SELECT count(1) FROM youtube WHERE monitor_count < #{monitorCount} del_flag = '0'")
    int countByMonitorCountLess(int monitorCount);

    @Select("SELECT max(monitor_count) FROM youtube WHERE del_flag = '0'")
    Integer maxOfMonitorCount();

    @Insert("INSERT INTO youtube (id, title, category, link, video_id, feedback_id, checked, check_error_times, check_time, error_msg, monitor_count, monitor_time, del_flag, create_time, update_time, remark) VALUES(#{id}, #{title}, #{category}, #{link}, #{videoId}, #{feedbackId}, #{checked}, #{checkErrorTimes}, #{checkTime}, #{errorMsg}, #{monitorCount}, #{monitorTime}, '0', now(), now(), #{remark})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(YoutubeLink youtubeLink);

    @Update("UPDATE youtube SET title=#{title}, category=#{category}, link=#{link}, video_id=#{videoId}, feedback_id=#{feedbackId}, checked=#{checked}, check_error_times=#{checkErrorTimes}, check_time=#{checkTime}, error_msg=#{errorMsg}, monitor_count=#{monitorCount}, monitor_time=#{monitorTime}, del_flag=#{delFlag}, create_time=now(), update_time=now(), remark=#{remark} WHERE id = #{id}")
    int reinsert(YoutubeLink youtubeLink);

    @Update("UPDATE youtube SET title=#{title}, category=#{category}, link=#{link}, video_id=#{videoId}, feedback_id=#{feedbackId}, checked=#{checked}, check_error_times=#{checkErrorTimes}, check_time=#{checkTime}, error_msg=#{errorMsg}, monitor_count=#{monitorCount}, monitor_time=#{monitorTime}, update_time=now(), remark=#{remark} WHERE id = #{id}")
    int update(YoutubeLink youtubeLink);

    @Update("UPDATE youtube SET checked=#{checked}, check_error_times=#{checkErrorTimes}, check_time=now(), error_msg=#{errorMsg}, remark=#{remark} WHERE id = #{id}")
    int updateCheckResult(YoutubeLink youtubeLink);

    /**
     * 提供给多画面监控用，多画面检测不通过，直接删除该链接。
     * @param id
     * @param reason
     * @return
     */
    @Update("UPDATE youtube SET checked='0', check_time=now(), error_msg=#{reason}, del_flag='1' update_time=now() WHERE id = #{id}")
    int offline(@Param("id")int id, @Param("reason")String reason);

    @Update("UPDATE youtube SET monitor_count=monitor_count+1, monitor_time=now() WHERE id = #{id}")
    int updateMonitor(int id);

    @Delete("UPDATE youtube SET del_flag = '1' WHERE id = #{id}")
    int deleteById(int id);

    @Delete("UPDATE youtube SET del_flag = '1' WHERE id = #{id}")
    int delete(YoutubeLink youtubeLink);
}
